package net.begincode.servlet.common;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;

import utils.WordThread;

public class ViewTask extends TimerTask {
	private ServletContext context = null;

	public ViewTask(ServletContext context) {
		this.context = context;
	}

	@Override
	public void run() {
		// 读取数据并清空
		List<String> words = WordThread.wordsReload();
		Map<String, Integer> viewMap = new HashMap<String, Integer>();
		// 修改浏览次数
		if (words != null) {
			for (String word : words) {
				if (viewMap.containsKey(word)) {
					viewMap.put(word, viewMap.get(word) + 1);
				} else {
					viewMap.put(word, 1);
				}
			}
		}
		if (viewMap != null && viewMap.size() > 0) {
			Set<String> key = viewMap.keySet();
			for (Iterator it = key.iterator(); it.hasNext();) {
				String s = (String) it.next();
				System.out.println(s + "值是：" + viewMap.get(s));
				String[] str = s.split(":");
				ViewServlet.updViewCount(str[0], str[1],
						Integer.parseInt(viewMap.get(s).toString()));
			}
		}

	}

}
